package com.Hsu.demo.mapper;

import com.Hsu.demo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author Hsu琛君珩
 * @ClassName:UserInfoMapper
 * @date 2023-12-24
 * @apiNote
 * @Version: v1.0
 */

@Mapper
public interface UserInfoMapper {

    /**
     * 查找
     * 方法1：对字段进行重命名
     * @return
     */
    @Select("select id, username, password, age, gender, phone," +
            " delete_flag as deleteFlag, create_time as createTime, update_time as updateTime" +
            " from userinfo")//注意这里换行后前面的from要加个空格，因为字符串拼接这里连起来的话这句sql语句就是错的
    List<UserInfo> selectAll();

    /**
     * 查找
     * 方法2：使用注解
     * @return
     */
    @Select("select * from userinfo")
    @Results(id="BaseMap",value = {
            // column="字段名",property="Java对象属性"
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    List<UserInfo> selectAll2();

    @ResultMap(value = "BaseMap")
    @Select("select * from userinfo where id=${id}")
    UserInfo selectOne(Integer id);

    @ResultMap(value = "BaseMap")
    @Select("select * from userinfo where username=#{username}")
    List<UserInfo> selectByName(String username);

    /**
     * 查找
     * 方法3：使用配置，自动转为驼峰
     * @return
     */
    @Select("select * from userinfo")
    List<UserInfo> selectAll3();

    @Select("select * from userinfo where id=#{userId}")
    UserInfo selectOne2(@Param("userId") Integer id);

    //设置返回自增id，把这个自增id赋值给 id字段
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into userinfo (username,password,age,gender,phone) " +
            "values (#{username},#{password},#{age},#{gender},#{phone})")
    Integer insert(UserInfo userInfo);

    //对insert的参数进行重命名
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into userinfo (username,password,age,gender,phone) " +
            "values (#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender},#{userInfo.phone})")
    Integer insert2(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from userinfo where id = #{id}")
    Integer delete(Integer id);

    @Update("update userinfo set age=#{age} where id=#{id}")
    Integer update(UserInfo userInfo);

    @Select("select * from userinfo order by id ${sort}")
    List<UserInfo> selectUserBySort(String sort);

    /**
     * 模糊查询
     * @param username
     * @return
     */
    @Select("select * from userinfo where username like concat('%',#{username},'%')")
    List<UserInfo> selectUserByLike(String username);
}
